package com.ytjj.qmyx.admin.service.impl;

import com.aliyun.oss.OSSClient;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.model.FileResponse;
import com.ytjj.common.utils.AliyunOSSClientUtil;
import com.ytjj.common.utils.DateTimeUtil;
import com.ytjj.qmyx.admin.dao.ProductDao;
import com.ytjj.qmyx.admin.mapper.ComplaintsRegistrationMapper;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.ComplaintsRegistration;
import com.ytjj.qmyx.admin.model.ComplaintsRegistrationExample;
import com.ytjj.qmyx.admin.model.request.complaints.ComplaintsRegistrationRequest;
import com.ytjj.qmyx.admin.model.response.complaints.ComplaintsRegistrationResponse;
import com.ytjj.qmyx.admin.service.WxComplaintsService;
import com.ytjj.qmyx.mall.client.MallClient;
import com.ytjj.qmyx.mall.mapper.ComplaintsRecordsMapper;
import com.ytjj.qmyx.mall.mapper.ProductMapper;
import com.ytjj.qmyx.mall.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WxComplaintsServiceImpl implements WxComplaintsService {

    @Resource
    private ComplaintsRecordsMapper complaintsRecordsMapper;
    @Resource
    private ComplaintsRegistrationMapper complaintsRegistrationMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private MallClient mallClient;

    @Override
    public CommonPage<WxComplaintsListResponse> wxComplaintsV3List(WxComplaintsListRequest request) {

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<WxComplaintsListResponse> list = complaintsRecordsMapper.wxComplaintsV3List(request);
        if (!CollectionUtils.isEmpty(list)) {
            List<Product> products = null;
            List<Integer> productIdList = list.stream().filter(item -> item.getProductId() != null).map(WxComplaintsListResponse::getProductId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(productIdList)) {
                ProductExample productExample = new ProductExample();
                productExample.createCriteria().andIdIn(productIdList);
                products = productMapper.selectByExample(productExample);
            }
            List<String> outTradeNoList = list.stream().map(WxComplaintsListResponse::getOutTradeNo).collect(Collectors.toList());
            List<ComplaintsMchIdResponse> complaintsMchIdResponses = complaintsRecordsMapper.queryMchId(outTradeNoList);

            for (WxComplaintsListResponse wxComplaintsListResponse : list) {
                if (!CollectionUtils.isEmpty(products)) {
                    List<Product> collect = products.stream().filter(item -> item.getId().equals(wxComplaintsListResponse.getProductId())).collect(Collectors.toList());
                    wxComplaintsListResponse.setSupplyProductId(CollectionUtils.isEmpty(collect) ? null : collect.get(0).getSupplyProductId());
                }
                if (!CollectionUtils.isEmpty(complaintsMchIdResponses)) {
                    List<ComplaintsMchIdResponse> collect = complaintsMchIdResponses.stream().filter(item -> item.getOutTradeNo().equals(wxComplaintsListResponse.getOutTradeNo())).collect(Collectors.toList());
                    wxComplaintsListResponse.setMchId(CollectionUtils.isEmpty(collect) ? null : collect.get(0).getMchId());
                }
            }

        }
        return CommonPage.restPage(list);
    }

    @Override
    public Integer saveComplaintsRegistration(ComplaintsRegistrationRequest request, Admin admin) {
        request.setAdminId(admin.getId());
//        ComplaintsRegistrationExample example = new ComplaintsRegistrationExample();
//        example.createCriteria().andComplaintIdEqualTo(request.getComplaintId());
//        List<ComplaintsRegistration> complaintsRegistrations = complaintsRegistrationMapper.selectByExample(example);
//        if (!CollectionUtils.isEmpty(complaintsRegistrations)) {
//            complaintsRegistrationMapper.delByComplaintId(request.getComplaintId());
//        }
        ComplaintsRegistration complaintsRegistration = new ComplaintsRegistration();
        BeanUtils.copyProperties(request,complaintsRegistration);
        complaintsRegistration.setCreateTime(new Date());
        complaintsRegistration.setType(request.getType() == null ? 1: Integer.valueOf(request.getType()));
        if (!StringUtils.isEmpty(request.getVerifyIssues())) {
            complaintsRegistration.setVerifyIssues(request.getVerifyIssues() +"==》 "+DateTimeUtil.getTime() +"  "+admin.getName() +" 提交");
        } else {
            complaintsRegistration.setVerifyIssues(null);
        }
        return complaintsRegistrationMapper.insert(complaintsRegistration);
    }

    @Override
    public Map<String,String> getImgUrl(MultipartFile multipartFile,String complaintId) throws Exception{
        OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
        FileResponse fileResponse = AliyunOSSClientUtil.uploadObject2OSS(ossClient, multipartFile, "images");
        CommonResult<String> result = mallClient.getImgUrl(fileResponse.getUrl(),complaintId);
        Map<String, String> map = new HashMap<>();
        map.put("url",fileResponse.getUrl());
        map.put("returnUrl",result.getData());
        return map;
    }

    @Override
    public List<ComplaintsRegistrationResponse> queryComplaintsRegistration(String complaintId) {
        List<ComplaintsRegistrationResponse> complaintsRegistrations = complaintsRegistrationMapper.queryComplaintsRegistration(complaintId);
        if (!CollectionUtils.isEmpty(complaintsRegistrations)) {
            complaintsRegistrations.stream().forEach( item ->{
                if (StringUtils.isEmpty(item.getVerifyIssues())) {
                    item.setVerifyIssues(item.getAdminName() + DateTimeUtil.getTime());
                }
            });
        }
        return complaintsRegistrations;
    }

    @Override
    public ComplaintsRecords queryByTradeNo(String tradeNo) {
        ComplaintsRecordsExample example = new ComplaintsRecordsExample();
        example.createCriteria().andOutTradeNoEqualTo(tradeNo);
        List<ComplaintsRecords> complaintsRecords = complaintsRecordsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(complaintsRecords) ? null : complaintsRecords.get(0);
    }
}
